import {
	funcVerify,
} from '@utils/router'
import {
	roleDelApi,
	roleFuncsApi,
	roleGrantFuncsApi,
} from '@utils/apis'

let _submitUrl = ''

module.exports = {

	roleAdd(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		comps.roleId.textbox('readonly', false)
		comps.roleForm.form('clear')
		comps.formDialog.dialog('open')
		_submitUrl = user.funcMap[func_id].func_url
	},

	roleEdit(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		let data = comps.dataGrid.datagrid('getSelected')
		if (!data) {
			$.messager.alert('警告', '请选择！');
			return false
		}
		comps.roleId.textbox('readonly', true)
		comps.roleForm.form('load', data)
		comps.formDialog.dialog('open')
		_submitUrl = user.funcMap[func_id].func_url
	},

	roleDel(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		let dataGrid = comps.dataGrid
		let data = dataGrid.datagrid('getSelected')
		if (!data) {
			$.messager.alert('警告', '请选择！', 'warning');
			return false
		}
		if (data.role_id == 'admin') {
			$.messager.alert('警告', '管理员不能删除！', 'error');
			return false
		}
		let role_id = data.role_id
		console.log('---data---', data)
		$.messager.confirm({
			title: '删除',
			msg: '确认删除？',
			fn: function(r) {
				if (r) {
					roleDelApi({
						role_id,
					}).then(r => {
						dataGrid.datagrid('reload');
					})
				}
			}
		});
	},

	submit(comps, user) {
		$.messager.progress();
		let {
			roleForm,
			dataGrid,
			formDialog,
		} = comps
		console.log('---_submitUrl---', _submitUrl)
		roleForm.form('submit', {
			url: _submitUrl,
			onSubmit: function() {
				let isValid = roleForm.form('validate');
				if (!isValid) {
					$.messager.progress('close');
				}
				_submitUrl = ''
				return isValid;
			},
			success: function(res) {
				console.log('---res---', res)
				try {
					res = JSON.parse(res)
				} catch (e) {

				}
				if (res.code != '000') {
					$.messager.alert(res.code, res.message)
				}
				$.messager.progress('close');
				dataGrid.datagrid('reload');
				formDialog.dialog('close');
				roleForm.form('clear')
			}
		})
	},

	cancel(dlg, form) {
		if (form)
			form.form('clear')
		if (dlg)
			dlg.dialog('close')
	},

	roleGrant(comps, user) {
		$._store.grant = false
		let datas = comps.roleFunc.treegrid('getRoots')
		let selectedUser = comps.dataGrid.datagrid('getSelected')
		if (!selectedUser)
			$.messager.alert('警告', '请选择！', 'warning');
		roleFuncsApi({
			role_id: selectedUser.role_id,
		}).then(res => {
			if (res.code != '000')
				return
			let {
				menuTree,
				moduleFuncMap,
				func_ids,
			} = res.result
			let data = getFuncTreeData(menuTree, moduleFuncMap, func_ids)
			comps.roleFunc.treegrid('loadData', data)
			// setFuncsTreeData(datas, comps.roleFunc, func_ids)
			comps.roleFuncDlg.dialog('open')
			$._store.grant = true
		})

	},

	roleFuncs(comps, user) {
		let {
			role_id,
		} = comps.dataGrid.datagrid('getSelected')
		if (role_id == 'admin') {
			comps.roleFuncDlg.dialog('close')
			return false
		}
		let nodeList = comps.roleFunc.treegrid('getCheckedNodes')
		let func_ids = nodeList.filter(val => val.func_id).map(val => val.func_id)
		return roleGrantFuncsApi({
			role_id,
			func_ids,
		}).then(res => {
			if (res.code == '000') {
				comps.roleFuncDlg.dialog('close')
			} else {
				$.messager.alert('警告', res.message, 'warning');
			}
		})
	},
}

function setFuncsTreeData(datas, roleFunc, func_ids) {
	for (let i = 0; i < datas.length; i++) {
		let data = datas[i]
		// console.log('---data---', data)
		if (Array.isArray(data.children)) {
			setFuncsTreeData(data.children, roleFunc, func_ids)
		} else {
			if (func_ids == -1 || func_ids.indexOf(data.id) > -1)
				roleFunc.treegrid('checkNode', data.id)
			else
				roleFunc.treegrid('uncheckNode', data.id)
		}
	}
}

function getFuncTreeData(menuTree, moduleFuncMap, role_func_ids = []) {
	let treeData = []
	for (let i = 0; i < menuTree.length; i++) {
		let module = menuTree[i]
		let data = {
			id: module.module_id,
			name: module.module_name,
			func_id: '',
			func_url: '',
			desc: '',
		}
		if (Array.isArray(module.children))
			data.children = getFuncTreeData(module.children, moduleFuncMap, role_func_ids)
		if (module.route) {
			let func_ids = Object.keys(moduleFuncMap[module.module_id])
			data.children = []
			for (let i = 0; i < func_ids.length; i++) {
				let func_id = func_ids[i]
				let func = moduleFuncMap[module.module_id][func_id]
				data.children.push({
					id: func.func_id,
					name: func.func_name,
					func_id: func.func_id,
					func_url: func.func_url,
					desc: func.desc,
					checked: role_func_ids.indexOf(func.func_id) > -1,
				})
			}
		}
		treeData.push(data)
	}
	return treeData
}